<template>
	<view class="info-card">
		<text :class="['introduce', isEllipsis ? 'u-line-4': '']">
			{{introduce}}
		</text>
		<view class="unfold-btn" v-if="showUnfoldBtn" @click="handleUnfold">
			<image :class="[isEllipsis ? '': 'unfold']" src="/static/imgs/icon-unfold.png" mode="scaleToFill"></image>
			<text>{{isEllipsis ? '展开': '收起'}}</text>
		</view>
		<view class="info-list">
			<view class="info-item">
				<image src="/static/imgs/info-phone.png" mode="scaleToFill"></image>
				<text>电话：{{telephone}}</text>
			</view>
			<view class="info-item">
				<image src="/static/imgs/info-site.png" mode="scaleToFill"></image>
				<text>官网：{{site}}</text>
			</view>
			<view class="info-item">
				<image src="/static/imgs/info-address.png" mode="scaleToFill"></image>
				<text>地址：</text>
				<text>{{address}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "InfoCard",
		props: {
			introduce: {
				type: String
			},
			
			telephone: {
				type: String
			},
			
			site: {
				type: String
			},
			
			address: {
				type: String
			}
		},
		data() {
			return {
				// 是否显示展开按钮
				showUnfoldBtn: false,
				// 是否省略文本
				isEllipsis: false,
			};
		},
		watch: {
			introduce(val) {
				if(val.trim()) {
					this.$nextTick(() => {
						this.getTextHeight()
					})
				}
			}
		},

		methods: {
			getTextHeight() {
				const query = uni.createSelectorQuery().in(this)
				query.select('.introduce').boundingClientRect(data => {
					if (data?.height > 84) {
						this.showUnfoldBtn = true
						this.isEllipsis = true
					}
				}).exec()
			},

			handleUnfold() {
				this.isEllipsis = !this.isEllipsis
			}
		}
	}
</script>

<style scoped lang="scss">
	.info-card {
		box-sizing: border-box;
		padding: 70rpx 40rpx;
		position: relative;
		width: 100%;
		border-radius: 14px;
		box-shadow: 0 0 4px 0 rgba($color: #000000, $alpha: .16);
		background-color: #fff;

		.introduce {
			display: block;
			line-height: 1.3125;
			white-space: pre-wrap;
		}

		.unfold-btn {
			margin-top: 12rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 12px;
			color: #C8C8C8;

			image {
				margin-right: 10rpx;
				width: 12px;
				height: 13px;

				&.unfold {
					transform: rotate(180deg);
				}
			}
		}
		
		.info-list{
			margin-top: 56rpx;
			.info-item{
				margin-top: 18rpx;
				display: flex;
				align-items: center;
				text{
					&:nth-child(2) {
						flex-shrink: 0;
					}
				}
				image{
					flex-shrink: 0;
					margin-right: 12rpx;
					width: 36rpx;
					height: 36rpx;
				}
			}
		}
	}
</style>
